package com.example.zyfypt516wh.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.zyfypt516wh.R;
import com.example.zyfypt516wh.adapter.ResAdapter;
import com.example.zyfypt516wh.bean.ResBean;
import com.example.zyfypt516wh.iface.ResListener;
import com.example.zyfypt516wh.model.ResModel;

import java.util.List;

public class Fragment32 extends BaseFragment {

    private List<ResBean> list;
    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;//显示布局效果
    private int page = 1; // 代表页数， 并初始化为 1， 代表第 1 页。
    private int lastVisibleItemPosition;//滚动刷新时， 最后一条可见条目的位置
    private ResAdapter adapter;
    private ResListener listener = new ResListener() {
        @Override
        public void onResponse(List<ResBean> beanlist) {
            if (page == 1)//第一页直接赋值
            {
                list = beanlist;
            } else {//其他页需要更新替换
                list.removeAll(beanlist);
                list.addAll(beanlist);
            }
            adapter.setList(list);
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context, "失败： " + msg, Toast.LENGTH_SHORT).show();
            System.out.println("--" + msg);
        }
    };

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment32, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        System.out.println("--32--" + getSessionID());
        initRecyclerView(view);
//        ResProjectModel model = new ResProjectModel();
        ResModel model=new ResModel();
        model.getSpecilList("video", page, getSessionID(), listener);
    }

    private void initRecyclerView(View view) {
        recyclerView = view.findViewById(R.id.rv32);
        layoutManager = new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManager);
        //每个 item 如果是确定高度， 设置此项提高性能
        recyclerView.setHasFixedSize(true);
        //实例化适配器
        adapter = new ResAdapter(context,"video");
        recyclerView.setAdapter(adapter);
        //列表滚动刷新
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (list != null) {
                    if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == list.size()) {
                        page += 1;
                        //再次实例化 ArticleModel， 调用方法获取网络数据， 请求新一页数据
                        ResModel model = new ResModel();
                        model.getSpecilList("video", page, getSessionID(), listener);
                    }
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();//滚动结束后将赋值为可见条目中最后一条位

            }
        });
    }
}
